﻿//-----------------------------------------------------------------------
// <copyright file="ShowSettingsWindowCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.UI.Windows.Settings
{
    using System.Windows;

    using ChatBox.DomainObjects.Settings;
    using ChatBox.Interfaces;
    using ChatBox.UI.Commands;
    using ChatBox.UI.Windows.Main;
    using ChatBox.Utility;

    /// <summary>
    /// Command to show the Settings window
    /// </summary>
    public class ShowSettingsWindowCommand : GenericCommandBase<MainWindowViewModel>
    {
        /// <summary>
        /// The source of server settings
        /// </summary>
        private readonly IServerSettingsRepository serverSettingsRepository;

        /// <summary>
        /// The source of user settings
        /// </summary>
        private readonly IUISettingsRepository uiSettingsRepository;

        /// <summary>
        /// Initializes a new instance of the ShowSettingsWindowCommand class
        /// </summary>
        /// <param name="serverSettingsRepository">The source of server settings</param>
        /// <param name="uiSettingsRepository">The source of ui settings</param>
        public ShowSettingsWindowCommand(IServerSettingsRepository serverSettingsRepository, IUISettingsRepository uiSettingsRepository)
        {
            this.serverSettingsRepository = serverSettingsRepository;
            this.uiSettingsRepository = uiSettingsRepository;
        }

        /// <summary>
        /// Execute the "show settings" command
        /// </summary>
        /// <param name="value">the view model</param>
        public override void ExecuteOnValue(MainWindowViewModel value)
        {
            SettingsWindow settingsWindow = new SettingsWindow();
            SettingsWindowViewModel viewModel = Locator.Resolve<SettingsWindowViewModel>();

            this.Populate(settingsWindow, viewModel);

            settingsWindow.DataContext = viewModel;
            viewModel.Window = settingsWindow;

            // after saving, update the main window view model with new settings
            // package the main window view model's Read settings command with the main window view model
            viewModel.NotifyApplicationOfSettingsChange = new GenericCommandWrapper<MainWindowViewModel, object>(value.RefreshSettingsCommand, value);

            settingsWindow.Loaded += (s, e) => settingsWindow.CenterInWindow(Application.Current.MainWindow);
            settingsWindow.ShowDialog();
        }

        /// <summary>
        /// Populate the window and view model
        /// </summary>
        /// <param name="window">the window to populate</param>
        /// <param name="viewModel">the view model to populate</param>
        private void Populate(SettingsWindow window, SettingsWindowViewModel viewModel)
        {
            viewModel.ServerSettings = this.serverSettingsRepository.ReadSettings().Copy();
            viewModel.UISettings = this.uiSettingsRepository.ReadSettings().Copy();

            viewModel.MinPublicChatRefreshInterval = ServerSettingsDataHelper.MinPublicChatRefreshInterval;
            viewModel.MaxPublicChatRefreshInterval = ServerSettingsDataHelper.MaxPublicChatRefreshInterval;

            viewModel.MinPrivateChatRefreshInterval = ServerSettingsDataHelper.MinPrivateChatRefreshInterval;
            viewModel.MaxPrivateChatRefreshInterval = ServerSettingsDataHelper.MaxPrivateChatRefreshInterval;

            viewModel.MinOtherUsersRefreshInterval = ServerSettingsDataHelper.MinOtherUsersRefreshInterval;
            viewModel.MaxOtherUsersRefreshInterval = ServerSettingsDataHelper.MaxOtherUsersRefreshInterval;

            window.themeCombo.ItemsSource = viewModel.ThemeManager.Themes;
        }
    }
}
